﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;
using Ryder_US_Website.BUS;

namespace Ryder_US_Website.Presentation.Admin
{
    public partial class Customer_List : System.Web.UI.Page
    {
        private String[] arr_trash = new String[2] { "Hoạt động", "Không hoạt động" };
        private List<RUS_Customer> list = new List<RUS_Customer>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["RUS_User"] == null)
            {
                Response.Redirect("Login.aspx");
            }
            list = CustomerBUS.GetAll();
            //add dynamic rows
            DataTable dt = new DataTable();
            dt.Columns.Add("#");
            dt.Columns.Add("Họ và tên");
            dt.Columns.Add("Tình trạng");
            DataRow row = null;
            for (int i = 0; i < list.Count; i++)
            {
                row = dt.NewRow();
                row["#"] = (i + 1).ToString();
                row["Họ và tên"] = "<a href='Customer_Update.aspx?id=" + list[i].Customer_ID + "'>" + list[i].Customer_name + "</a>";
                dt.Rows.Add(row);
            }
            grvCustomer.DataSource = dt;
            grvCustomer.DataBind();
        }

        protected void grvCustomer_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = e.Row.RowIndex;
                e.Row.Cells[1].Text = Server.HtmlDecode(e.Row.Cells[1].Text);

                //Link button xóa
                LinkButton lkbtn = new LinkButton();
                lkbtn.Text = arr_trash[list[i].Customer_trash];
                lkbtn.Command += new CommandEventHandler(lkbtnDelete_Click);
                lkbtn.CommandArgument = list[i].Customer_ID.ToString();
                e.Row.Cells[2].Controls.Add(lkbtn);
            }
        }
        protected void lkbtnDelete_Click(object sender, CommandEventArgs e)
        {
            try
            {
                CustomerBUS.DeleteRestore(int.Parse(e.CommandArgument.ToString()));
            }
            catch(Exception ex)
            {
                Response.Write(Utils.UtilHelpers.ShowMessage(ex.Message));
            }
            Page_Load(sender, e);
        }

        protected void grvCustomer_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvCustomer.PageIndex = e.NewPageIndex;
            grvCustomer.DataBind();
        }

        
    }
}
